package com.wsx.sky.http.controller;

import com.google.gson.Gson;
import com.wsx.sky.http.HttpClientUtils;
import com.wsx.sky.http.bean.HttpRequest;
import com.wsx.sky.http.bean.HttpResponse;
import com.wsx.sky.http.service.HttpServerFeign;
import com.wsx.sky.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.http.controller.ClientController
 * @Description: TODO
 * @date 2018/12/10
 */
@RestController
@RequestMapping("/http/client")
public class ClientController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private HttpServerFeign httpServerFeign;

    private static final String POST_URL = "http://127.0.0.1:8132/http/server/request/feign";
    private static final String GET_URL = "http://127.0.0.1:8132/http/server/get/feign/";
    private Gson gson = new Gson();


    @RequestMapping(
            value = "/request/http",
            method = {RequestMethod.POST}
    )
    public HttpResponse http(@RequestBody HttpRequest httpRequest) {
        System.out.println(httpRequest.getTraceid());
        if (null == httpRequest
                || StringUtils.isEmpty(httpRequest.getName())) {
            return new HttpResponse().buildCode(40001);
        }
        if ("hello".equals(httpRequest.getName())) {
            return new HttpResponse().buildMsg("你不知道");
        }
        String value = redisService.getString(httpRequest.getName());
        if (StringUtils.isEmpty(value)) {
            try {
                if (httpRequest.getName().equals("wang")) {
                    value = HttpClientUtils.execPost(POST_URL, gson.toJson(httpRequest), null);
                } else {
                    value = HttpClientUtils.get(GET_URL + httpRequest.getName(), null);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            if (!StringUtils.isEmpty(value)) {
                String msg = null;
                if (httpRequest.getName().equals("wang")) {
                    HttpResponse response = gson.fromJson(value, HttpResponse.class);
                    msg = response.getMsg();
                } else {
                    msg = value;
                }
                redisService.setString(httpRequest.getName(), msg);

                return new HttpResponse().buildMsg(msg);
            }

        }

        return new HttpResponse().buildCode(50001);
    }

    @RequestMapping(
            value = "/request/feign",
            method = {RequestMethod.POST}
    )
    public HttpResponse feign(@RequestBody HttpRequest httpRequest) {
        try {
            return httpServerFeign.feign(httpRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HttpResponse().buildCode(50001).buildMsg("client 错误");
    }

    @RequestMapping(
            value = "/get/feign/{name}",
            method = {RequestMethod.GET}
    )
    public HttpResponse get(@PathVariable("name") String name) {
        try {
            return httpServerFeign.get(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HttpResponse().buildCode(50001).buildMsg("client 错误");
    }


    @RequestMapping(
            value = "/redis/{key}",
            method = {RequestMethod.GET}
    )
    public HttpResponse redis(@PathVariable("key") String key) {
        if (StringUtils.isEmpty(key)) {
            return new HttpResponse().buildCode(40001);
        }
        String value = redisService.getString(key);
        if (StringUtils.isEmpty(value)) {
            return new HttpResponse().buildCode(50001);
        }
        return new HttpResponse().buildMsg(value);
    }
}
